package cn.zhj.base.mvp.ui

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import cn.zhj.base.R
import com.bumptech.glide.Glide

abstract class EasyDialog : Dialog {
    var layout: View? = null
    var layoutId: Int = 0
    var mContext: Context


    constructor(mContext: Context, mLayoutId: Int) : super(mContext, R.style.EasyDialog) {
        this.mContext = mContext;
        this.layoutId = mLayoutId;
        initView();
    }

    private fun initView() {
        val infalater: LayoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        layout = infalater.inflate(layoutId , null);
        this.addContentView(layout , ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
        val holder = DialogHolder(context , layout);
        convert(holder);
    }


    abstract fun convert(holder: DialogHolder)

    class DialogHolder(context: Context , view:View?){
        var mContext: Context? = context
        var view: View? = view

        fun <T : View?> getView(viewId: Int): T {
            var view: View? = view?.findViewById(viewId)?:null
            return view as T
        }

        fun setText(viewId: Int , text:String?) : DialogHolder{
            var tv  = getView<TextView>(viewId)
            tv.text = text?:""
            return this
        }

        fun setTextCorlor(viewId: Int, colorId: Int): DialogHolder {
            val view = getView<TextView>(viewId)
            view.setTextColor(ContextCompat.getColor(mContext!!, colorId))
            return this
        }


        fun setVisiable(viewId: Int , mVisiable:Boolean) : DialogHolder{
            val view = getView<View>(viewId)
            if (mVisiable) {
                view.visibility = View.VISIBLE
            } else {
                view.visibility = View.GONE
            }
            return this
        }

        fun setImageUrl(viewId: Int , url :String):DialogHolder{
            val iv  = getView<ImageView>(viewId)
            Glide.with(mContext!!).load(url).into(iv)
            return this;
        }

        fun setImageResource(viewId: Int , resId :Int):DialogHolder{
            val iv  = getView<ImageView>(viewId)
            Glide.with(mContext!!).load(resId).into(iv)
            return this;
        }

        fun setOnClickListener(viewId: Int , listener:View.OnClickListener):DialogHolder{
            val view = getView<View>(viewId)
            view.setOnClickListener(listener)
            return this
        }

        fun setBackgroundCorlor(viewId: Int, colorId: Int): DialogHolder? {
            val view = getView<View>(viewId)
            view.setBackgroundColor(ContextCompat.getColor(mContext!!, colorId))
            return this
        }

        fun setAlpha(alpha: Float): DialogHolder? {
            view?.alpha = alpha
            return this
        }
    }

}